package main

import (
	"github.com/gogap/logs"
	"sync"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/logic"
	"yundian/internel/app/models/statistics"
	"yundian/internel/pkg/dbs"
	"yundian/internel/pkg/dingo"
)

func main()  {
	dingo.InitViper("./configs/adm.yaml")
	dbs.NewMysqlComponent().Step()
	dbs.NewRedisComponent().Step()

	now := time.Now()
	startDay := now.AddDate(0, 0, -45).Format("2006-01-02")
	endDay := now.AddDate(0, 0, -16).Format("2006-01-02")
	days := helper.GetDayRangeTime(startDay, endDay)

	statisticsuserByTypeByHaveOrders(days, 405886)
}


func statisticsuserByTypeByHaveOrders(dateList []string, id int) ( r bool) {
	for _, d := range dateList {
		var tempOrder, tempDividends, tempUserRel bool
		var tempWg sync.WaitGroup
		tempWg.Add(3)
		go func() {
			defer tempWg.Done()
			tempOrder = dao.NewStatisticsDao().InsertOrderToTemp(d).Unwrap().(bool)
		}()
		go func() {
			defer tempWg.Done()
			tempDividends = dao.NewStatisticsDao().InsertDividendsToTemp(d).Unwrap().(bool)
		}()
		go func() {
			defer tempWg.Done()
			tempUserRel = dao.NewStatisticsDao().InsertOrdersUserRelevancyToTemp(d).Unwrap().(bool)
		}()
		tempWg.Wait()

		if tempOrder && tempDividends && tempUserRel {
			orderAndIncomeStatisticsUser(d, id)
			dao.NewStatisticsDao().DeleteTempTable(d) // 删除临时表
			logs.Info("日期", d, "缓存表已清除")
		} else {
			logs.Info("日期", d, "缓存表生成失败")
		}
	}
	return true
}

func orderAndIncomeStatisticsUser(dateStr string, id int) (ok bool) {
	m := &statistics.StatisticsUserModel{}
	var mEnd, mComplete, mCancel, mSy dao.CountSettlement
	var mAllDraw, mDraw, mNoDraw, mSmallDraw, mcNodraw, cNodraw dao.CountSettlement
	var totalDevices, totalIncome int64
	var directUsers, directPlaces, totalUsers, totalPlaces, userIds []int
	var wx, qrcode, qrcodeWait, qrcodeSuccess, qrcodeFail dao.CountSettlement
	var placeSales int64

	userIds = append(userIds, id)

	var w sync.WaitGroup
	w.Add(4)
	go func() {
		defer w.Done()
		directUsers = dao.NewStatisticsDao().CountUsersByParentId(userIds, dateStr).Unwrap().([]int) // 直属团队数
		directPlaces = dao.NewStatisticsDao().CountPlacesByUserId(id, dateStr).Unwrap().([]int) // 直属门店数
		totalUsers = logic.NewTaskStatisticsUserLogic().GetTotalTeams(id, dateStr) // 团队总数
		totalPlaces = dao.NewStatisticsDao().CountPlacesByUserIds(totalUsers, dateStr).Unwrap().([]int) // 门店总数
		totalDevices = dao.NewStatisticsDao().CountDevicesByPlaceIds(totalPlaces).Unwrap().(int64) // 设备总数
	}()
	go func() {
		defer w.Done()
		mEnd = dao.NewStatisticsDao().QueryUserTotalOrderByState(0, id, dateStr).Unwrap().(dao.CountSettlement)
		mComplete = dao.NewStatisticsDao().QueryUserTotalOrderByState(5, id, dateStr).Unwrap().(dao.CountSettlement)
		mCancel = dao.NewStatisticsDao().QueryUserTotalOrderByState(3, id, dateStr).Unwrap().(dao.CountSettlement)
		mSy = dao.NewStatisticsDao().QueryUserTotalOrderByState(7, id, dateStr).Unwrap().(dao.CountSettlement)

		totalIncome = dao.NewStatisticsDao().QueryUserTotalIncomeByUserId(id, dateStr).Unwrap().(int64)
	}()
	go func() {
		defer w.Done()
		mAllDraw = dao.NewStatisticsDao().QueryUserTotalOrderByDraw(4, 5, id, dateStr).Unwrap().(dao.CountSettlement)
		mDraw = dao.NewStatisticsDao().QueryUserTotalOrderByDraw(1, 5, id, dateStr).Unwrap().(dao.CountSettlement)
		mNoDraw = dao.NewStatisticsDao().QueryUserTotalOrderByDraw(2, 0, id, dateStr).Unwrap().(dao.CountSettlement)
		mSmallDraw = dao.NewStatisticsDao().QueryUserTotalOrderByDraw(3, 5, id, dateStr).Unwrap().(dao.CountSettlement)
		mcNodraw = dao.NewStatisticsDao().QueryUserTotalOrderByDraw(2, 5, id, dateStr).Unwrap().(dao.CountSettlement)
		cNodraw = dao.NewStatisticsDao().QueryUserTotalOrderByDraw(2, 3, id, dateStr).Unwrap().(dao.CountSettlement)
	}()
	go func() {
		defer w.Done()
		// 获取门店动销
		placeSales = dao.NewStatisticsDao().QueryPlaceSalesByUserId(dateStr, id).Unwrap().(int64)
		wx = logic.NewTaskStatisticsUserLogic().QueryTotalCashLog(dateStr, 0, 0, id)
		qrcode = logic.NewTaskStatisticsUserLogic().QueryTotalCashLog(dateStr, 2, 0, id)
		qrcodeWait = logic.NewTaskStatisticsUserLogic().QueryTotalCashLog(dateStr, 2, 1, id)
		qrcodeSuccess = logic.NewTaskStatisticsUserLogic().QueryTotalCashLog(dateStr, 2, 2, id)
		qrcodeFail = logic.NewTaskStatisticsUserLogic().QueryTotalCashLog(dateStr, 2, 3, id)
	}()
	w.Wait()

	m.DateStr = dateStr // 日期
	m.UserId = int64(id) // 用户ID

	m.TotalTeam = len(totalUsers) // 团队总数
	m.TotalPlace = len(totalPlaces) // 门店总数
	m.TotalDirectPlace = len(directPlaces)  // 直属下级
	m.TotalDirectTeam = len(directUsers)  // 直属门店
	m.TotalDevices = totalDevices // 设备总数

	m.TotalOrders = mEnd.Total          // 当天订单数
	m.TotalComplete = mComplete.Total   // 当天完成的订单数
	m.TotalCancel = mCancel.Total       // 当天撤销的订单数
	m.TotalSy = mSy.Total               // 当天守约的订单数
	m.TotalCompleteNodraw = mcNodraw.Total
	m.TotalCancelNodraw = cNodraw.Total

	m.TotalAllDraw = mAllDraw.Total     // 总抽单数
	m.TotalDraw = mDraw.Total           // 普通 抽单数
	m.TotalSmalldraw = mSmallDraw.Total
	m.TotalNodraw = mNoDraw.Total       // 未抽单数

	m.TotalIncome = totalIncome                 // 用户收益

	m.PlaceSales = placeSales // 门店动销数

	m.SettlementTotal = mEnd.Money
	m.SettlementComplete = mComplete.Money
	m.SettlementCancel = mCancel.Money
	m.SettlementNormalTotal = mcNodraw.Money
	m.SettlementCancelNodraw = cNodraw.Money

	m.SettlementAlldraw = mAllDraw.Money
	m.SettlementDraw = mDraw.Money
	m.SettlementNodraw = mNoDraw.Money
	m.SettlementSmalldraw = mSmallDraw.Money

	m.WeixinTotal = wx.Total
	m.WeixinMoney = wx.Money
	m.QrcodeTotal = qrcode.Total
	m.QrcodeSuccessTotal = qrcodeSuccess.Total
	m.QrcodeSuccessMoney = qrcodeSuccess.Money
	m.QrcodeWaitTotal = qrcodeWait.Total
	m.QrcodeWaitMoney = qrcodeWait.Money
	m.CashFailedTotal = qrcodeFail.Total
	m.CashFailedMoney = qrcodeFail.Money

	ok = dao.NewStatisticsDao().InsertStatisticsUser(m).Unwrap().(bool)
	return
}
